﻿using Dotnet.TopApi;
using Dotnet.TopApi.Request;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Dotnet.Sui
{
    //GET https://www.sui.com/category/member.do HTTP/1.1
    public class DoMemberRequest : SuiRequest, ITopRequest<DoMemberResponse>
    {
        public string GetApiName()
        {
            return "category/member.do";
        }

        public IDictionary<string, string> GetParameters()
        {
            TopDictionary dict = base.GetParameters1() as TopDictionary;
            dict.AddHeadersMethod("GET");
            return dict;
        }

        public void Validate()
        {
           // throw new NotImplementedException();
        }
    }

    public class DoMemberResponse : SuiDoResponse
    {
        public override void ParseCateAccount(string html)
        {
            _cateAccount=MemberCategory(html);
        }
        private List<Account> MemberCategory(string html)
        {
            List<Account> list = new List<Account>();
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var docNode = doc.DocumentNode;
            var firstNodes = docNode.CssSelect("div.list-box>div.lb-row").ToList();
            foreach (var node in firstNodes)
            {
                string id = node.Id.Replace("category", "");
                string name = node.CssSelect("li.li-level2").First().InnerText;
                Console.WriteLine($"{id}  {name}  0");
                list.Add(new Account() { Id = id, Name = name, ChildAccount = null });
            }
            return list;
        }
    }
}
